/**
 * Created by Tuffy on 16/1/29.
 */
'use strict';

angular.module('starter.controllers')

.controller('DistributionDetailCtrl', ['$scope', '$state','DistributionService', function($scope, $state, DistributionService) {

    var distributionId = $state.params.id;

    $scope.distributionDetail = {
        roomType: '房型A',
        roomNo: '301',
        bookUser: '谷佳良',
        userTel: '18523418882',
        orderTime: '2016年02月12日 12:12',
        amount: 120,
        payType: '到付',
        checkInTime: '2016年02月15日',
        checkOutTime: '2016年02月16日',
        orderStatus: 1,
        from: '阿里旅行',
    };

    /**
     * distribution-hotels
     */
    $scope.distribution ={
        search: {
            value:'',
            doAct:function(){
                if(this.timer){
                    $timeout.cancel(this.timer);
                }
                this.timer = $timeout(function(){
                    $scope.distribution.page.pageNum = 1;
                    $scope.distribution.data = [];
                    $scope.distribution.loadData();
                },500);
            }
        },
        page: {
            pageNum:1,
            pageSize:10,
            total:200
        },
        loaded:false,
        data:[],
        loadData:function(){
            var _this = this,
                params = {
                    hotelName:_this.search.value,
                    pageNum:_this.page.pageNum,
                    pageSize:_this.page.pageSize
                };
            _this.loaded = false;
            for(var key in _this.search.filter){
                params[key] = _this.search.filter[key];
            }
            DistributionService.findHotels(params)
                .success(function(res){
                    if(res.success){
                        _this.data = _this.data.concat(res.attribute.rows);
                        _this.page.total = res.attribute.total;
                    }else{
                        _this.page.total = 0;
                        console.log('加载失败');
                    }
                }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                });
        },
        moreDataCanBeLoaded:function(){
            return this.page.total > this.page.pageNum*this.page.pageSize;
        },
        loadMore:function(){
            this.page.pageNum++;
            this.loadData();
        },
        refresh:function(){
            this.page.pageNum = 1;
            this.data = [];
            this.loadData();
        }
    } ;

    $scope.distribution.loadData();
}])
.service('DistributionService',['CrmHttp','$rootScope',function(CrmHttp, $rootScope){
    var url = '/crm/crm-hotel-public/';
    return {
        /**
         * 分销查询
         * @param params
         * @returns {*}
         */
        findHotels: function (params) {
            var act = 'getHotels';
            params['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        }
    };
}])